{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5e69309f-c830-4c53-a1e7-8ebd97e18d9d",
   "metadata": {},
   "source": [
    "# 一、fuzzywuzzy 测试样例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d5a91ef4-b815-43bb-88c7-319d2c3e5ae9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "其他相关的句子1 60\n",
      "其他相关的句子2 60\n"
     ]
    }
   ],
   "source": [
    "# 不使用结巴分词的样例\n",
    "import jieba\n",
    "from fuzzywuzzy import process\n",
    "\n",
    "# 准备文档\n",
    "documents = [\n",
    "    \"第十五届粤、京、港、沪铁道学会学术年会暨第八届世界轨道交通发展研究会年会\",\n",
    "    \"其他相关的句子1\",\n",
    "    \"其他相关的句子2\"\n",
    "]\n",
    "\n",
    "# 分词函数\n",
    "def segment(text):\n",
    "    return list(jieba.cut(text))\n",
    "\n",
    "# 搜索函数\n",
    "def search(query):\n",
    "    results = process.extract(query, documents, limit=None)\n",
    "    return [(doc,score) for doc, score in results if score > 50]  # 根据需要调整阈值\n",
    "\n",
    "# 输入搜索内容\n",
    "search_query = \"他句子\"\n",
    "matched_docs = search(search_query)\n",
    "\n",
    "for doc,score in matched_docs:\n",
    "    print(doc,score)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8a0752cc-4488-4937-914e-f78f0f1b662f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('第十五届粤、京、港、沪铁道学会学术年会暨第八届世界轨道交通发展研究会年会', 86), ('其他相关的句子1', 0), ('其他相关的句子2', 0)]\n",
      "第十五届粤、京、港、沪铁道学会学术年会暨第八届世界轨道交通发展研究会年会\n"
     ]
    }
   ],
   "source": [
    "# 使用结巴分词的样例\n",
    "import jieba\n",
    "from fuzzywuzzy import process\n",
    "\n",
    "# 准备文档\n",
    "documents = [\n",
    "    \"第十五届粤、京、港、沪铁道学会学术年会暨第八届世界轨道交通发展研究会年会\",\n",
    "    \"其他相关的句子1\",\n",
    "    \"其他相关的句子2\"\n",
    "]\n",
    "\n",
    "# 分词函数\n",
    "def segment(text):\n",
    "    return list(jieba.cut(text))\n",
    "\n",
    "# 搜索函数\n",
    "def search(query):\n",
    "    # 对查询字符串进行分词\n",
    "    segmented_query = \" \".join(segment(query))\n",
    "    results = process.extract(segmented_query, documents, limit=None)\n",
    "    print(results)\n",
    "    return [doc for doc, score in results if score > 50]  # 根据需要调整阈值\n",
    "\n",
    "# 输入搜索内容\n",
    "search_query = \"粤、京\"\n",
    "matched_docs = search(search_query)\n",
    "\n",
    "for doc in matched_docs:\n",
    "    print(doc)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "dea1acc8-f9a4-4753-87a6-97611afa9792",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2007民族和地域建筑文化可持续发展论坛', '第二十届中国国际医用仪器设备展览会暨技术交流会、中放磁共振年会暨国际磁共振研讨会', '第十三届全国反应堆结构力学会议', '第四届中国矿山安全技术装备与管理大会', '第八届全国超导学术研讨会', '首届全国中西医结合医学美容学术交流会', '中华护理学会全国护理管理改革创新高层论坛', '实验动物科技创新与发展学术研讨会', '第三届全国气象观测技术交流会', '第十五届粤、京、港、沪铁道学会学术年会暨第八届世界轨道交通发展研究会年会']\n",
      "[('Altair 2015 技术大会', 85.5, 10562), ('The 6th International Conference on Nanoscience and Technology, China 2015 (第六届中国国际纳米科学技术会议)', 85.5, 27020), ('China Marketing International Conference 2015 (2015中国市场营销国际学术年会)', 85.5, 35780), ('黑龙江省高等教育学会2015年学术年会', 71.05263157894737, 35639), ('中国核学会2015年学术年会', 70.71428571428571, 26891), ('广西药学会2015年学术年会', 70.71428571428571, 31359), ('江苏省高等教育学会2015年学术年会', 70.00000000000001, 1075), ('中国地质学会2015年学术年会', 66.00000000000001, 4135), ('宁夏烟草学会2015年学术年会', 66.00000000000001, 7304), ('广西公路学会2015年学术年会', 66.00000000000001, 20246), ('中国海洋学会2015年学术年会', 66.00000000000001, 23212), ('2013年药膳学术年会', 65.45454545454547, 3236), ('江苏省学前教育学会2015年学术年会', 65.0, 807), ('江苏省高等教育学会2016年学术年会', 65.0, 6273), ('江苏省高等教育学会2018年学术年会', 65.0, 20333), ('四川省高等教育学会2012年学术年会', 65.0, 25881), ('四川省高等教育学会2014年学术年会', 65.0, 38318), ('中国核学会2019年学术年会', 64.28571428571429, 26429), ('中国水利学会2015学术年会', 64.28571428571429, 36611), ('江苏省高等教育学会2014学术年会', 63.52941176470588, 23616)]\n"
     ]
    }
   ],
   "source": [
    "# 使用结巴分词的样例\n",
    "import jieba\n",
    "from rapidfuzz import process, fuzz\n",
    "\n",
    "# 准备文档\n",
    "path2 = r\"C:\\Users\\Administrator\\Documents\\WXWork\\1688853051339318\\Cache\\File\\2024-10\\wanfang\"\n",
    "with open(path2,\"r\",encoding=\"utf-8\") as f:\n",
    "    documents = [line.strip() for line in f.readlines()]\n",
    "\n",
    "print(documents[:10])\n",
    "\n",
    "# 分词函数\n",
    "def segment(text):\n",
    "    return list(jieba.cut(text))\n",
    "\n",
    "# 搜索函数\n",
    "def search(query):\n",
    "    # 对查询字符串进行分词\n",
    "    segmented_query = \" \".join(segment(query))\n",
    "    results = process.extract(segmented_query, documents, scorer=fuzz.WRatio, limit=20)\n",
    "    return results  # 根据需要调整阈值\n",
    "\n",
    "results = search(\"黑龙江省高等教育学会2015年学术年会暨理事工作会会议\")\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25312f3d-0dd8-4d10-a5e2-92bdf342ee64",
   "metadata": {},
   "source": [
    "# 二、rapidfuzz 简单使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6c721103-f881-4e94-962f-f5a9899a9de3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('New York Jets', 76.92307692307692, 1),\n",
       " ('New York Giants', 64.28571428571428, 2)]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from rapidfuzz import process, fuzz\n",
    "choices = [\"Atlanta Falcons\", \"New York Jets\", \"New York Giants\", \"Dallas Cowboys\"]\n",
    "process.extract(\"new york jets\", choices, scorer=fuzz.WRatio, limit=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f83155d0-9557-4e0b-90f0-ac7e6233c9a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Dallas Cowboys', 83.07692307692308, 3)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "process.extractOne(\"cowboys\", choices, scorer=fuzz.WRatio)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e0a0e49a-fc9d-4631-894d-5f50029c8a80",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('New York Jets', 100.0, 1), ('New York Giants', 78.57142857142857, 2)]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from rapidfuzz import process, fuzz, utils\n",
    "process.extract(\"new york jets\", choices, scorer=fuzz.WRatio, limit=2, processor=utils.default_process)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "42dd921f-d76b-4b56-907f-6ca5233eecb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Dallas Cowboys', 90.0, 3)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "process.extractOne(\"cowboys\", choices, scorer=fuzz.WRatio, processor=utils.default_process)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95af72c1-7ecd-4205-add2-2b23f3814a34",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
